package com.szxw.BaiduAi.mapper.impl;

import com.szxw.BaiduAi.mapper.EmotionMapper;
import com.szxw.BaiduAi.pojo.Emotions;
import com.szxw.BaiduAi.pojo.Items;
import com.szxw.BaiduAi.service.EmotionService;
import com.szxw.BaiduAi.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.util.*;
import java.sql.SQLException;

/**
 * @Author: 苏执小威
 * @Email:1410511702@qq.com
 * @Desc:
 * @Date: 2022/9/19 16:59
 */
public class EmotionImpl implements EmotionMapper {

    private QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());

    @Override
    public int insertEmotion(Emotions emotions) {
        String sql = "INSERT INTO emotion(`sentiment`,`confidence`,`positive_prob`,`negative_prob`,`text`)VALUES(?,?,?,?,?)";
        try {
            int update = queryRunner.update(sql,
                    new Object[]{emotions.getItems().get(0).getSentiment(),
                            emotions.getItems().get(0).getConfidence(),
                            emotions.getItems().get(0).getPositive_prob(),
                            emotions.getItems().get(0).getNegative_prob(),
                            emotions.getText()
                    });
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Items> selectEmotion(String text) {
        String sql = "SELECT * FROM emotion WHERE `text` = ?";
        try {
            List<Items> emotions =  queryRunner.query(sql,new BeanListHandler<>(Items.class),text);
            return emotions;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
